/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.comboBox;

import java.util.Locale;

import javax.swing.DefaultComboBoxModel;

import org.jxSwing.component.comboBox.cellRenderer.LocaleListRenderer;
import org.jxSwing.component.comboBox.cellRenderer.LocaleListRenderer.Type;
import org.jxUtils.check.CheckUtil;
import org.jxUtils.lang.StringUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.comboBox
 * @date 15/06/2014 - 15:19:44
 */
public class LocaleComboBox extends ComboBox<Locale> {
	
	private static final long serialVersionUID = 9199360771782440441L;
	
	private static final Locale[] AVAILABLE_LOCALES = Locale.getAvailableLocales();
	
	/**
	 * 
	 */
	public LocaleComboBox() {
	
		this(Type.DISPLAY_LANGUAGE);
	}
	
	/**
	 * @param type
	 */
	public LocaleComboBox(final Type type) {
	
		super();
		
		sort(AVAILABLE_LOCALES, type);
		
		setModel(new DefaultComboBoxModel<Locale>(AVAILABLE_LOCALES));
		
		setRenderer(new LocaleListRenderer(type));
		
		setSelectedItem(Locale.getDefault());
	}
	
	/**
	 * @param array
	 * @param type
	 */
	public static final void sort(final Locale[] array, final Type type) {
	
		//
		if(CheckUtil.isNotNull(array)) {
			
			Locale aux;
			
			//
			for(int i = array.length; i >= 1; i--) {
				
				//
				for(int j = 1; j < i; j++) {
					
					final Locale localeJ_1 = array[j - 1];
					final Locale localeJ = array[j];
					
					final String countryJ_1 = localeJ_1.getCountry().isEmpty() ? "" : "-" + localeJ_1.getCountry();
					final String countryJ = localeJ.getCountry().isEmpty() ? "" : "-" + localeJ.getCountry();
					
					String valueJ_1 = "";
					String valueJ = "";
					
					//
					if(type.equals(Type.COUNTRY) || type.equals(Type.DISPLAY_COUNTRY) || type.equals(Type.ISO3_COUNTRY)) {
						
						//
						valueJ_1 = type.equals(Type.COUNTRY) ? localeJ_1.getCountry() : type.equals(Type.DISPLAY_COUNTRY) ? localeJ_1.getDisplayCountry() : localeJ_1.getISO3Country();
						valueJ = type.equals(Type.COUNTRY) ? localeJ.getCountry() : type.equals(Type.DISPLAY_COUNTRY) ? localeJ.getDisplayCountry() : localeJ.getISO3Country();
						
					}else {
						
						//
						valueJ_1 = (type.equals(Type.LANGUAGE) ? localeJ_1.getLanguage() : type.equals(Type.DISPLAY_LANGUAGE) ? localeJ_1.getDisplayLanguage() : localeJ_1.getISO3Language()) + countryJ_1;
						valueJ = (type.equals(Type.LANGUAGE) ? localeJ.getLanguage() : type.equals(Type.DISPLAY_LANGUAGE) ? localeJ.getDisplayLanguage() : localeJ.getISO3Language()) + countryJ;
					}
					
					valueJ_1 = StringUtil.normalize(valueJ_1);
					valueJ = StringUtil.normalize(valueJ);
					
					//
					if(valueJ_1.compareTo(valueJ) > 0) {
						
						aux = array[j];
						
						array[j] = array[j - 1];
						
						array[j - 1] = aux;
					}
				}
			}
		}
	}
}
